package cn.sdhuijin.healthexam.control;

import java.util.Properties;

import android.app.Activity;
import android.util.Log;
import android.widget.CheckBox;
import cn.sdhuijin.healthexam.util.Constants;

public class CheckBoxViewControl implements IControl {

	private Activity activity;
	private int[] checkBoxIds;
	private String name;

	public CheckBoxViewControl(Activity activity, int[] checkBoxIds, String name) {
		this.activity = activity;
		this.checkBoxIds = checkBoxIds;
		this.name = name;
	}

	@Override
	public void getValue(Properties prop) {
		try {
			StringBuffer buffer = new StringBuffer();
			StringBuffer buffer_id = new StringBuffer();
			for (int i = 0; i < checkBoxIds.length; i++) {
				CheckBox checkbox = (CheckBox) activity
						.findViewById(checkBoxIds[i]);
				if (checkbox.isChecked()) {
					buffer.append(checkbox.getText().toString());
					buffer.append(Constants.SPLIT_TAG);
					buffer_id.append(i + 1);
					buffer_id.append(Constants.SPLIT_TAG);
				}
			}
			if (buffer != null && !Constants.STR_NULL.equals(buffer.toString())) {
				prop.setProperty(this.name + "_content", buffer.toString());
				prop.setProperty(this.name, buffer_id.toString());
			} else {
				prop.setProperty(this.name + "_content", "");
				prop.setProperty(this.name, "");
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	@Override
	public void setValue(Properties prop) {
		try {
			String value = prop.getProperty(this.name + "_content");
			if (value != null) {
				String[] str = value.split(Constants.SPLIT_TAG);
				for (int i = 0; i < checkBoxIds.length; i++) {
					for (int j = 0; j < str.length; j++) {
						CheckBox checkBox = (CheckBox) activity
								.findViewById(checkBoxIds[i]);
						if (str[j].equals(checkBox.getText().toString())) {
							checkBox.setChecked(true);
						}
					}
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}
